/*     */ package com.zimbra.qa.unittest;
/*     */ 
/*     */ import com.zimbra.client.ZMailbox;
/*     */ import com.zimbra.client.ZMessage;
/*     */ import com.zimbra.client.ZSearchParams;
/*     */ import com.zimbra.client.ZSearchParams.Cursor;
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.mailbox.Message;
/*     */ import com.zimbra.soap.type.SearchSortBy;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Collections;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import junit.framework.TestCase;
/*     */ import org.junit.Test;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TestSearchSortByDate
/*     */   extends TestCase
/*     */ {
/*  43 */   private static final String NAME_PREFIX = TestSearchSortByDate.class.getSimpleName();
/*     */   private static final String RECIPIENT = "search_test_1";
/*     */   private static final String RECIPIENT2 = "search_test_2";
/*     */   private static final String SENDER = "user1";
/*     */   private static final String PASSWORD = "test123";
/*     */   private static final String SEARCH_STRING = "jkfheowerklsaklasdqweds";
/*     */   
/*     */   public void setUp() throws Exception
/*     */   {
/*  52 */     cleanUp();
/*     */     
/*  54 */     Map<String, Object> attrs = new HashMap();
/*  55 */     attrs.put("zimbraMailHost", LC.zimbra_server_hostname.value());
/*  56 */     attrs.put("zimbraAttachmentsIndexingEnabled", Boolean.valueOf(true));
/*  57 */     attrs.put("cn", "search_test1");
/*  58 */     attrs.put("displayName", "TestAccount unit test user 1");
/*  59 */     Provisioning.getInstance().createAccount(TestUtil.getAddress("search_test_1"), "test123", attrs);
/*     */     
/*  61 */     attrs = new HashMap();
/*  62 */     attrs.put("zimbraMailHost", LC.zimbra_server_hostname.value());
/*  63 */     attrs.put("zimbraAttachmentsIndexingEnabled", Boolean.valueOf(false));
/*  64 */     attrs.put("cn", "search_tes2t");
/*  65 */     attrs.put("displayName", "TestAccount unit test user 2");
/*  66 */     Provisioning.getInstance().createAccount(TestUtil.getAddress("search_test_2"), "test123", attrs);
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessagesSortDateAsc()
/*     */   {
/*     */     try {
/*  73 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test_1");
/*  74 */       long timestamp = System.currentTimeMillis() - 216000000L;
/*  75 */       int numMessages = 3309;
/*  76 */       int firstLimit = 100;
/*  77 */       int incLimit = 50;
/*  78 */       int offset = 0;
/*  79 */       ArrayList<String> expectedIds = new ArrayList();
/*  80 */       for (int i = 0; i < numMessages; i++) {
/*  81 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_1"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/*  82 */         expectedIds.add(Integer.toString(msg.getId()));
/*  83 */         timestamp += 1000L;
/*     */       }
/*     */       
/*     */ 
/*  87 */       Thread.sleep(100L);
/*  88 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_1");
/*  89 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/*  90 */       searchParams.setSortBy(SearchSortBy.dateAsc);
/*  91 */       searchParams.setLimit(firstLimit);
/*  92 */       searchParams.setTypes("message");
/*  93 */       List<String> msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/*  94 */       assertEquals(firstLimit, msgIds.size());
/*  95 */       int gotMessages = msgIds.size();
/*  96 */       List<String> seenIds = new ArrayList();
/*  97 */       seenIds.addAll(msgIds);
/*  98 */       int recCount = 1;
/*  99 */       offset += firstLimit;
/* 100 */       while (gotMessages > 0) {
/* 101 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 102 */         searchParams.setSortBy(SearchSortBy.dateAsc);
/* 103 */         searchParams.setLimit(incLimit);
/* 104 */         searchParams.setTypes("message");
/* 105 */         searchParams.setOffset(offset);
/* 106 */         msgIds = TestUtil.searchMessageIds(zmbx, searchParams);
/* 107 */         recCount++;
/* 108 */         gotMessages = msgIds.size();
/* 109 */         int resCount = 0;
/* 110 */         for (String szId : msgIds) {
/* 111 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), szId, Integer.valueOf(seenIds.indexOf(szId)) }), seenIds.contains(szId));
/* 112 */           seenIds.add(szId);
/* 113 */           resCount++;
/*     */         }
/*     */         
/* 116 */         offset += incLimit;
/*     */       }
/* 118 */       for (int i = 0; i < expectedIds.size(); i++) {
/* 119 */         assertEquals("IDs at index " + i + " do not match", (String)expectedIds.get(i), (String)seenIds.get(i));
/*     */       }
/* 121 */       assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 123 */       e.printStackTrace();
/* 124 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 126 */       e.printStackTrace();
/* 127 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessagesCloseTimestamps()
/*     */   {
/*     */     try {
/* 135 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test_1");
/* 136 */       long timestamp = System.currentTimeMillis();
/* 137 */       int numMessages = 2207;
/* 138 */       int firstLimit = 100;
/* 139 */       int incLimit = 50;
/* 140 */       int offset = 0;
/* 141 */       ArrayList<String> expectedIds = new ArrayList();
/* 142 */       for (int i = 0; i < numMessages; i++) {
/* 143 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_1"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/* 144 */         expectedIds.add(Integer.toString(msg.getId()));
/*     */       }
/*     */       Message msg;
/* 147 */       for (int i = 0; i < numMessages; i++) {
/* 148 */         msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_1"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, "this message does not contains a search string which we are searching for", timestamp);
/*     */       }
/*     */       
/* 151 */       Collections.reverse(expectedIds);
/* 152 */       Thread.sleep(100L);
/* 153 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_1");
/* 154 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 155 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/* 156 */       searchParams.setLimit(firstLimit);
/* 157 */       searchParams.setTypes("message");
/* 158 */       List<ZMessage> resultSet = TestUtil.search(zmbx, searchParams);
/* 159 */       assertEquals(firstLimit, resultSet.size());
/* 160 */       int gotMessages = resultSet.size();
/* 161 */       List<String> seenIds = new ArrayList();
/* 162 */       ZMessage lastHit = null;
/* 163 */       for (ZMessage m : resultSet) {
/* 164 */         seenIds.add(m.getId());
/* 165 */         lastHit = m;
/*     */       }
/* 167 */       int recCount = 1;
/* 168 */       offset += firstLimit;
/* 169 */       while (gotMessages > 0) {
/* 170 */         searchParams.setCursor(new ZSearchParams.Cursor(lastHit.getId(), Long.toString(lastHit.getReceivedDate())));
/* 171 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 172 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 173 */         searchParams.setLimit(incLimit);
/* 174 */         searchParams.setTypes("message");
/* 175 */         searchParams.setOffset(offset);
/* 176 */         resultSet = TestUtil.search(zmbx, searchParams);
/* 177 */         recCount++;
/* 178 */         gotMessages = resultSet.size();
/* 179 */         int resCount = 0;
/* 180 */         for (ZMessage msg : resultSet) {
/* 181 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), msg.getId(), Integer.valueOf(seenIds.indexOf(msg.getId())) }), seenIds.contains(msg.getId()));
/* 182 */           seenIds.add(msg.getId());
/* 183 */           resCount++;
/* 184 */           lastHit = msg;
/*     */         }
/*     */         
/* 187 */         offset += incLimit;
/*     */       }
/* 189 */       assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 191 */       e.printStackTrace();
/* 192 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 194 */       e.printStackTrace();
/* 195 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   @Test
/*     */   public void testMessagesCloseTimestamps2()
/*     */   {
/*     */     try {
/* 203 */       Mailbox recieverMbox = TestUtil.getMailbox("search_test_2");
/* 204 */       long timestamp = System.currentTimeMillis();
/* 205 */       int numMessages = 4307;
/* 206 */       int firstLimit = 100;
/* 207 */       int incLimit = 50;
/* 208 */       int offset = 0;
/* 209 */       ArrayList<String> expectedIds = new ArrayList();
/* 210 */       for (int i = 0; i < numMessages; i++) {
/* 211 */         Message msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_2"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, String.format("this message contains a search string %s which we are searching for and a number %d and timestamp %d", new Object[] { "jkfheowerklsaklasdqweds", Integer.valueOf(i), Long.valueOf(timestamp) }), timestamp);
/* 212 */         expectedIds.add(Integer.toString(msg.getId()));
/* 213 */         timestamp += 10L;
/*     */       }
/*     */       Message msg;
/* 216 */       for (int i = 0; i < numMessages; i++) {
/* 217 */         msg = TestUtil.addMessage(recieverMbox, TestUtil.getAddress("search_test_1"), TestUtil.getAddress("user1"), NAME_PREFIX + " testing bug " + i, "this message does not contains a search string which we are searching for", timestamp);
/*     */       }
/* 219 */       Collections.reverse(expectedIds);
/* 220 */       Thread.sleep(100L);
/* 221 */       ZMailbox zmbx = TestUtil.getZMailbox("search_test_2");
/* 222 */       ZSearchParams searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 223 */       searchParams.setSortBy(SearchSortBy.dateDesc);
/* 224 */       searchParams.setLimit(firstLimit);
/* 225 */       searchParams.setTypes("message");
/* 226 */       List<ZMessage> resultSet = TestUtil.search(zmbx, searchParams);
/* 227 */       assertEquals(firstLimit, resultSet.size());
/* 228 */       int gotMessages = resultSet.size();
/* 229 */       List<String> seenIds = new ArrayList();
/* 230 */       ZMessage lastHit = null;
/* 231 */       for (ZMessage m : resultSet) {
/* 232 */         seenIds.add(m.getId());
/* 233 */         lastHit = m;
/*     */       }
/* 235 */       int recCount = 1;
/* 236 */       offset += firstLimit;
/* 237 */       while (gotMessages > 0) {
/* 238 */         searchParams.setCursor(new ZSearchParams.Cursor(lastHit.getId(), Long.toString(lastHit.getReceivedDate())));
/* 239 */         searchParams = new ZSearchParams("in:inbox jkfheowerklsaklasdqweds");
/* 240 */         searchParams.setSortBy(SearchSortBy.dateDesc);
/* 241 */         searchParams.setLimit(incLimit);
/* 242 */         searchParams.setTypes("message");
/* 243 */         searchParams.setOffset(offset);
/* 244 */         resultSet = TestUtil.search(zmbx, searchParams);
/* 245 */         recCount++;
/* 246 */         gotMessages = resultSet.size();
/* 247 */         int resCount = 0;
/* 248 */         for (ZMessage msg : resultSet) {
/* 249 */           assertFalse(String.format("Request %d, result %d, encountered duplicate ID %s. Previously seen at %d", new Object[] { Integer.valueOf(recCount), Integer.valueOf(resCount), msg.getId(), Integer.valueOf(seenIds.indexOf(msg.getId())) }), seenIds.contains(msg.getId()));
/* 250 */           seenIds.add(msg.getId());
/* 251 */           resCount++;
/* 252 */           lastHit = msg;
/*     */         }
/*     */         
/* 255 */         offset += incLimit;
/*     */       }
/* 257 */       assertEquals("Returned incorrect number of messages", numMessages, seenIds.size());
/*     */     } catch (ServiceException e) {
/* 259 */       e.printStackTrace();
/* 260 */       fail(e.getMessage());
/*     */     } catch (Exception e) {
/* 262 */       e.printStackTrace();
/* 263 */       fail(e.getMessage());
/*     */     }
/*     */   }
/*     */   
/*     */   public void tearDown()
/*     */     throws Exception
/*     */   {
/* 270 */     cleanUp();
/*     */   }
/*     */   
/*     */   private void cleanUp() throws Exception {
/* 274 */     Provisioning prov = Provisioning.getInstance();
/* 275 */     Account account = prov.get(Key.AccountBy.name, TestUtil.getAddress("search_test_1"));
/* 276 */     if (account != null) {
/* 277 */       prov.deleteAccount(account.getId());
/*     */     }
/*     */     
/* 280 */     account = prov.get(Key.AccountBy.name, TestUtil.getAddress("search_test_2"));
/* 281 */     if (account != null) {
/* 282 */       prov.deleteAccount(account.getId());
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/qa/unittest/TestSearchSortByDate.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */